package k3_IO.p2_fileStream.demo01;

import java.io.*;

public class ObjectStream {

    public static void objectOutputStream() throws Exception{

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://obj.txt"));

//        oos.writeInt(100);
//        oos.writeUTF("hello world");
//        oos.writeUTF("hello world");
        oos.writeObject(new Employee("tom","hrb",20));
//        oos.writeObject(new Employee("jerry","上海",30));

        oos.flush();
        oos.close();

    }

    public static void objectInputStream() throws Exception{

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E://obj.txt"));

        //序列化的时候，对象顺序可以任意，反序列化的时候，读取对象的顺序必须与序列化的顺序保持一致
        Employee employee = (Employee) ois.readObject();
        System.out.println(employee);

        ois.close();


    }

    public static void main(String[] args) throws Exception {

        ObjectStream.objectOutputStream();
        ObjectStream.objectInputStream();

    }
}
